function timestampToTime() {
	var date = new Date();
	var Y = date.getFullYear();
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
	var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
	var hh = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
	var mm = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
	var ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
	return Y + '-' + M + '-' + D + ' ' + hh + ':' + mm + ':' + ss;
}


// 最大日期  最小日期
function chooseTimed() {
	let date = new Date();
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();
	if (month < 10) {
		month = '0' + month;
	}
	if (day < 10) {
		day = '0' + day;
	}
	this.defaultDate = year + '-' + month + '-' + day;
	let minyear = year - 1;
	// this.minDate = minyear+'-'+month+'-'+day;
	let maxyear = year + 1;
	this.maxDate = maxyear + '-' + month + '-' + day;

	this.minDate = this.lastYearDate(year + '-' + month + '-' + day);
}
// 返回去年日期
function lastYearDate(d) {
	const date = new Date(d);
	const year = date.getFullYear();
	const month = date.getMonth() + 1;
	const day = date.getDate();
	const lastYear = year - 1;
	const thisYearisLeap = this.isLeap(year);
	const lastYearIsLeap = this.isLeap(lastYear);
	// 特殊情况：去年是否是闰年 或 今年是闰年且为12月31日
	if (lastYearIsLeap || (thisYearisLeap && month == 12 && day == 31)) return this.getDay(d, -366);
	return this.getDay(d, -365);
}
// 是否闰年
function isLeap(year) {
	return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
// 获取日期
function getDay(d, num) {
	const date = new Date(new Date(d).setDate(new Date(d).getDate() + num));
	const year = date.getFullYear();
	const month = (date.getMonth() + 1 + '').padStart(2, '0');
	const day = (date.getDate() + '').padStart(2, '0');
	return `${year}-${month}-${day}`;
}

function getNum(maxArr) {
	// let maxArr = this.dataEcharts[this.valueKey][0].data;
	let num = '';
	let newArr = maxArr.map(Number); // 字符串数组转为整数数组
	let max = newArr[0];
	for (let i = 0, len = newArr.length; i < len; i++) {
		if (max < newArr[i]) {
			max = newArr[i];
		}
	}
	if (max >= 5) {
		num = 5;
	} else if (max >= 4) {
		num = 4;
	} else if (max >= 3) {
		num = 3;
	} else if (max >= 2) {
		num = 2;
	} else if (max >= 0) {
		num = 1;
	}
	return num;
}
// 时间戳转日期
function timestampToDateFormat(timestamp) {
	console.log(timestamp);
	const dateObj = new Date(timestamp); // 创建Date对象
	const year = String(dateObj.getFullYear()); // 获取年份
	const month = String(dateObj.getMonth() + 1).padStart(2, "0"); // 获取月份，并补零
	const day = String(dateObj.getDate()).padStart(2, "0"); // 获取日期，并补零

	return year + "-" + month + "-" + day; // 返回转换后的日期格式
}

function dateFormatToTimestamp(data) {
	const date = new Date(data); // 获取当前日期对象
	const timestamp = date.getTime(); // 获取时间戳
	return timestamp;
}

// 获取下一年的今天日期
function getNextYearToday() {
	const today = new Date();
	const nextYear = new Date(today.getFullYear() + 1, today.getMonth(), today.getDate());
	let year = nextYear.getFullYear();
	let month = String(nextYear.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要+1，并且补0
	let day = String(nextYear.getDate()).padStart(2, '0'); // 补0

	return `${year}-${month}-${day}`;
}

// 文件判断显示预览图
function fileImgFunc(file) {
	var fileExtension = file.file_suffix

	console.log('File format:', fileExtension);
	let word = require('@/static/file_icon/word.png');
	let cad = require('@/static/file_icon/cad.png');
	let excel = require('@/static/file_icon/excel.png');
	let pdf = require('@/static/file_icon/pdf.png');
	let ppt = require('@/static/file_icon/ppt.png');
	let zip = require('@/static/file_icon/zip.png');
	let other = require('@/static/file_icon/other.png');
	let video = require('@/static/file_icon/video.png');
	switch (fileExtension) {
		case 'doc':
			file.src = word;
			break;
		case 'docx':
			file.src = word;
			break;
		case 'pptx':
			file.src = ppt;
			break;
		case 'pptm':
			file.src = ppt;
			break;
		case 'ppt':
			file.src = ppt;
			break;
		case 'pdf':
			file.src = pdf;
			break;
		case 'xls':
			file.src = excel;
			break;
		case 'xlsx':
			file.src = excel;
			break;
		case 'zip':
			file.src = zip;
			break;
		case 'dwg':
			file.src = cad;
			break;
		case 'dxf':
			file.src = cad;
			break;
		case 'dwt':
			file.src = cad;
			break;
		case 'dwf':
			file.src = cad;
			break;
		default:
			let img_type_arr = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp']
			let video_type_arr = ['mp4', 'avi', 'mkv', 'rmvb', 'MOV', 'm4v']

			if (img_type_arr.indexOf(fileExtension) != -1) {
				file.src = file.url || file.file_url;
			} else if (video_type_arr.indexOf(fileExtension) != -1) {
				file.src = video
			} else {
				file.src = other
			}

	}
	return file.src;
}

export {
	timestampToTime,
	getNum,
	timestampToDateFormat,
	getNextYearToday,
	fileImgFunc
}